<?php

namespace app\app\controller;

use app\app\model\ProjectMatterModel;
use app\app\model\ProjectModel;
use app\app\model\ProjectTDModel;
use app\BaseController;
use app\Request;

class ProjectPower extends BaseController
{

    /**停电列表
     * @param Request $request
     */


    public function analysis()
    {
        $type = input('type', '4');
        if ($type == 1) { //当天
            $start_time = date('Y-m-d 00:00:00', time());
            $end_time = date('Y-m-d 23:59:59', time());
            $where[] = ['start_date', '<=', $end_time];
            $where[] = ['end_date', '>=', $start_time];
        } elseif ($type == 2) {  //本月
            $start_time = date('Y-m-d 00:00:00', strtotime("this week Monday", time()));
            $end_time = date("Y-m-d 23:59:59", strtotime("$start_time +6 day"));
            $where[] = ['start_date', '<=', $end_time];
            $where[] = ['end_date', '>=', $start_time];
        } elseif (($type == 3)) { //本月
            $date = date("Y-m-d");
            $start_time = date('Y-m-01 00:00:00', strtotime($date));
            $end_time = date('Y-m-d 23:59:59', strtotime("$start_time +1 month -1 day"));
            $where[] = ['start_date', '<=', $end_time];
            $where[] = ['end_date', '>=', $start_time];
//            下月
//            $date = date("Y-m-d");
//            $firstday = date('Y-m-01', strtotime($date));
//            $start_time = date('Y-m-01', strtotime("$firstday +1 month"));
//            $end_time = date('Y-m-d', strtotime("$firstday +2 month -1 day"));
//            $where[] = ['start_date', '<=', $end_time];
//            $where[] = ['end_date', '>=', $start_time];


        } elseif ($type == 4) {
            $start_time = date('Y-01-01 00:00:00', time());
            $end_time = date('Y-12-31 23:59:59', time());
            $where[] = ['start_date', '<=', $end_time];
            $where[] = ['end_date', '>=', $start_time];
        } else {

        }
        $td_num = ProjectTDModel::where($where)->count();
        $five_num = ProjectTDModel::where($where)->where('is_high_risk', '是')->count();
//
//        //本月停电总数
//        $date = date("Y-m-d");
//        $firstday = date('Y-m-01', strtotime($date));
//        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
//
//        $where_month[] = ['start_date', '<=', $lastday];
//        $where_month[] = ['end_date', '>=', $firstday];
//        $td_month = ProjectTDModel::where($where_month)->count();
//
//
//        //下月停电总数
//        $next_first = date('Y-m-01', strtotime("$firstday +1 month"));
//        $next_last = date('Y-m-d', strtotime("$firstday +2 month -1 day"));
//        $where_month_next[] = ['start_date', '<=', $next_last];
//        $where_month_next[] = ['end_date', '>=', $next_first];
//        $next_month = ProjectTDModel::where($where_month_next)->count();
//
//        $start_time_year = date('Y-01-01 00:00:00', time());
//        $end_time_year = date('Y-12-31 23:59:59', time());
//
//        $where_year[] = ['start_date', '<=', $end_time_year];
//        $where_year[] = ['end_date', '>=', $start_time_year];
//        $year = ProjectTDModel::where($where_year)->count();

        $res = array(
            'td_num' => $td_num,
            'five_num' => $five_num,

            'td_month' => $td_num,
            'next_month' => $td_num,
            'year' => $td_num
        );
        $this->success('success', $res);
    }

    public function index(Request $request)
    {
        $data = $request->param();
        $state = input('state', '1');

        //城市查询
        if (isset($data['city']) && !empty($data['city'])) {
            $city_arr = explode(',', $data['city']);
            $where[] = ['a.city', 'IN', $city_arr];;
        }

        //项目名查询
        if (isset($data['project_name']) && !empty($data['project_name'])) {
            $where[] = ['b.project_name|a.project_name', 'like', '%' . $data['project_name'] . '%'];
        }

        //当日停电条件

        if (isset($data['start_time']) || isset($data['end_time'])) {
            if (isset($data['start_time']) && isset($data['end_time'])) {
                $where[] = ['c.start_date', '<=', $data['end_time']];
                $where[] = ['c.end_date', '>=', $data['start_time']];
            } else {
                if (isset($data['start_time'])) $where[] = ['c.start_date', '>', $data['start_time']];
                if (isset($data['end_time'])) $where[] = ['c.end_date', '<', $data['end_time']];
            }
        } else {
            if ($state == 1) {
                $start_time = date('Y-m-d 00:00:00', time());
                $end_time = date('Y-m-d 23:59:59', time());
                $where[] = ['c.start_date', '<=', $end_time];
                $where[] = ['c.end_date', '>=', $start_time];
            } elseif ($state == 2) {
                //本周
                $start_time = date('Y-m-d 00:00:00', strtotime("this week Monday", time()));
                $end_time = date("Y-m-d 23:59:59", strtotime("$start_time +6 day"));
                $where[] = ['c.start_date', '<=', $end_time];
                $where[] = ['c.end_date', '>=', $start_time];
            } elseif (($state == 3)) {  //本月
                $date = date("Y-m-d");
                $start_time = date('Y-m-01', strtotime($date));
                $end_time = date('Y-m-d', strtotime("$start_time +1 month -1 day"));
                $where[] = ['c.start_date', '<=', $end_time];
                $where[] = ['c.end_date', '>=', $start_time];

//                下月
//                $date = date("Y-m-d");
//                $firstday = date('Y-m-01', strtotime($date));
//                $start_time = date('Y-m-01', strtotime("$firstday +1 month"));
//                $end_time = date('Y-m-d', strtotime("$firstday +2 month -1 day"));
//                $where[] = ['start_date', '<=', $end_time];
//                $where[] = ['end_date', '>=', $start_time];
            } elseif ($state == 4) {  //本年
                $start_time = date('Y-01-01 00:00:00', time());
                $end_time = date('Y-12-31 23:59:59', time());
                $where[] = ['c.start_date', '<=', $end_time];
                $where[] = ['c.end_date', '>=', $start_time];
            } else {

            }
        }
        $is_high_risk = input('is_high_risk', '');
        if ($is_high_risk) {
            $where[] = ['c.is_high_risk', '=', '是'];
        }


        $list = ProjectModel::alias('a')
            ->field('c.id as risk_id,a.id,a.project_name,a.link_project_id,b.project_name as project_main_name,b.id as main_id,c.start_date,c.end_date')
            ->join('t_project b', 'a.link_project_id = b.id')
            ->join('t_project_td c', 'a.id = c.project_id')
//            ->group('a.project_name')
            ->where($where)->select()->toArray();

        $info = $list;

        $tmp_arr = array();
        $temp = array();
        foreach ($info as $k => $v) {
            if (!in_array($v['main_id'], $tmp_arr)) {
                $tmp_arr[$k] = $v['main_id'];
                $temp[$k]['id'] = $v['main_id'];
                $temp[$k]['project_name'] = $v['project_main_name'];
            }
        }
        foreach ($temp as $key => $value) {
            foreach ($info as $k => $v) {
                if ($value['id'] == $v['link_project_id']) {
                    $temp[$key]['children'][] = $v;
                }
            }
            $list['data'] = $temp;
        }

        $res = [];
        if ($list) $res = array_values($list['data']);

        $this->success('success', $res);

    }

    public function list()
    {
        $id = input('id', '');
        $list = ProjectTDModel::where('project_id', $id)->select();
        $this->success('success', $list);
    }

    public function info()
    {
        $id = input('id', '');
        $list = ProjectTDModel::alias('a')->field('a.*,b.file_name,b.file_path')
            ->where('a.id', $id)
            ->leftjoin('sys_file_info b', 'b.file_id = a.file_id')
            ->find();
        $this->success('success', $list);
    }


}
